package com.tuozixuan.api.impl;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.nacos.api.annotation.NacosInjected;
import com.alibaba.nacos.api.config.ConfigService;
import com.alibaba.nacos.api.config.annotation.NacosValue;
import com.alibaba.nacos.api.exception.NacosException;
import com.alibaba.nacos.api.naming.NamingService;
import com.alibaba.nacos.api.naming.pojo.Instance;
import com.tuozixuan.api.DemoApi;
import com.tuozixuan.config.ProxyProperties;

@RestController
@RefreshScope
public class DemoApiImpl implements DemoApi {

	@NacosValue(value = "${useLocalCache:false}", autoRefreshed = true)
	@Value(value = "${useLocalCache:false}")
	private boolean useLocalCache;
	
	@Autowired
	private ProxyProperties proxyProperties;
	
	@NacosInjected
	private NamingService namingService;
	
	@NacosInjected
    private ConfigService configService;

    public String hello(String msg, HttpServletRequest request) {
        String h = request.getHeader("X-Request-Foo");
        return "hello " + msg + " " + h;
    }

	@Override
	public boolean get() {
		// update: curl -X POST "http://localhost:8848/nacos/v1/cs/configs?dataId=WARE_ID&group=DEFAULT_GROUP&content=useLocalCache=true"
		// update: curl -X POST "http://localhost:8848/nacos/v1/cs/configs?dataId=springcloud-nacos-server.properties&group=DEFAULT_GROUP&content=useLocalCache=true"
		return useLocalCache;
	}

	@Override
	public String getProxyConfig() {
		// curl -X POST "http://192.168.101.47:8848/nacos/v1/cs/configs?dataId=springcloud-nacos-server-dev.properties&group=DEFAULT_GROUP&content=proxy.host=192.168.101.175"
		return proxyProperties.getHost() + ":" + proxyProperties.getPort();
	}

	@Override
	public List<Instance> getInstance(String serviceName) throws NacosException {
		return namingService.getAllInstances(serviceName);
	}

    
}
